package com.example.todolist.database.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

@Entity(
    tableName = "todo",
    indices = [Index(value = ["category_id"])],  // 添加索引
    foreignKeys = [
        ForeignKey(
            entity = CategoryEntity::class,           // 引用的目标实体类
            parentColumns = ["category_id"],             // 目标表的主键列
            childColumns = ["category_id"],      // 当前表的外键列
            onDelete = ForeignKey.CASCADE,      // 删除规则（级联删除）
            onUpdate = ForeignKey.CASCADE       // 更新规则（级联更新）
        )
    ]
)
data class TodoEntity(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Int = 0,

    @ColumnInfo(name = "category_id")
    var categoryId: Int = -1,

    @ColumnInfo(name = "title")
    var title: String = "",

    @ColumnInfo(name = "username")
    var username: String = "",

    @ColumnInfo(name = "end_time")
    var endTime: String = "",

    @ColumnInfo(name = "importance")
    var importance: Int = 1,

    @ColumnInfo(name = "detail")
    var detail: String = "",

    @ColumnInfo(name = "is_finish")
    var isFinish: Boolean = false,

    @ColumnInfo(name = "is_expired")
    var isExpired: Boolean = false,

    @ColumnInfo(name = "order_num")
    var orderNum: Int = 0,

    @ColumnInfo(name = "create_time")
    var createTime: String = SimpleDateFormat(
        "yyyy年MM月dd日 HH:mm:ss",
        Locale.getDefault()
    ).format(Date())

) {
    // 显式无参构造函数
    @Ignore
    constructor() : this(
        id = 0,
        categoryId = -1,
        title = "",
        username = "",
        endTime = "",
        importance = 1,
        detail = "",
        isFinish = false,
        isExpired = false,
        orderNum = 0,
        createTime = SimpleDateFormat(
            "yyyy年MM月dd日 HH:mm:ss",
            Locale.getDefault()
        ).format(Date())
    )
}